const path = require('path');

// NOTE 在 sass 中通过别名（@ 或 ~）引用需要指定路径
const sassImporter = function(url) {
  if (url[0] === '~' && url[1] !== '/') {
    return {
      file: path.resolve(__dirname, '..', 'node_modules', url.substr(1))
    }
  }

  const reg = /^@styles\/(.*)/
  return {
    file: reg.test(url) ? path.resolve(__dirname, '..', 'src/styles', url.match(reg)[1]) : url
  }
}

const config = {
  projectName: 'qian_shop',
  date: '2020-4-27',
  designWidth: 750,
  deviceRatio: {
    '640': 2.34 / 2,
    '750': 1,
    '828': 1.81 / 2
  },
  h5: {
     esnextModules: ['taro-ui']
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  // outputRoot: `dist/${process.env.NODE_ENV}/${process.env.TARO_ENV}`,
  babel: {
    sourceMap: true,
    presets: [
      ['env', {
        modules: false
      }]
    ],
    plugins: [
      'transform-decorators-legacy',
      'transform-class-properties',
      'transform-object-rest-spread',
      ['transform-runtime', {
          helpers: false,
          polyfill: false,
          regenerator: true,
          moduleName: 'babel-runtime'
        }
      ]
    ],
  },
  // plugins: {
  //   sass: {
  //     importer: sassImporter
  //   }
  // },
  plugins: [
    '@tarojs/plugin-uglify',
    '@tarojs/plugin-sass',
    // '@tarojs/plugin-less'
  ],
  //设置打包过程中的 JS 代码压缩
  uglify: {
    enable: true,
    config: {
      warnings: false,
      // 配置项同 https://github.com/mishoo/UglifyJS2#minify-options
      compress: {
        drop_debugger: true,
        drop_console: true,
      },
    }
  },
  //设置打包过程中的 CSS 代码压缩
  csso: {
    enable: true,
    config: {
      // 配置项同 https://github.com/css/csso#minifysource-options
    }
  },
  // ?vframe/jpg/offset/0/w/240/h/300
  defineConstants: {
    SIZE:10,
    Version:'"1.0.0"',
    VIDEO_SUFFIX:'"-shipin.jpg"',
    LOGO_SPLIT:'"-logo.webp"',
    LOGO_JPG_SPLIT:'"-logo.jpg"',
    DETAIL_SPLIT:'"-detail.webp"',
    DETAIL_JPG_SPLIT:'"-detail.jpg"',
    QIANYOUZHUO_SPLIT:'"-qianyouzhuo.webp"',
  },
  copy: {
    patterns: [
      // {
      //   from: 'src/wxComponents/vant-weapp/wxs/',
      //   to: 'dist/wxComponents/vant-weapp/wxs/'
      // },
      // {
      //   from: 'src/wxComponents/vant-weapp/dropdown-menu/',
      //   to: 'dist/wxComponents/vant-weapp/dropdown-menu/'
      // },
      // {
      //   from: 'src/wxComponents/vant-weapp/dropdown-item/',
      //   to: 'dist/wxComponents/vant-weapp/dropdown-item/'
      // }
    ],
    options: {}
  },
  alias: {
    // '@actions': path.resolve(__dirname, '..', 'src/actions'),
    // '@assets': path.resolve(__dirname, '..', 'src/assets'),
    // '@components': path.resolve(__dirname, '..', 'src/components'),
    // '@constants': path.resolve(__dirname, '..', 'src/constants'),
    // '@reducers': path.resolve(__dirname, '..', 'src/reducers'),
    // '@styles': path.resolve(__dirname, '..', 'src/styles'),
    // '@utils': path.resolve(__dirname, '..', 'src/utils')
  },
  mini: {
    //小程序编译过程的相关配置。
    // compile: {
    //   compressTemplate: true,//打包时是否需要压缩 wxml
    // },
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
          browsers: [
            'last 3 versions',
            'Android >= 4.1',
            'ios >= 8'
          ]
        }
      },
      pxtransform: {
        enable: true,
        config: {
          selectorBlackList: [
            /^.van-.*?$/, 
          ]    
        }
      },
      url: {
        enable: true,
        config: {
          limit: 10240 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
          browsers: [
            'last 3 versions',
            'Android >= 4.1',
            'ios >= 8'
          ]
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      },
    },
    esnextModules: ['taro-ui']
    // sassLoaderOption: {
    //   importer: sassImporter
    // }
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
